package com.yuyou.fn.portal.service.impl;

import com.yuyou.fn.portal.service.IOuterCampusService;
import com.yuyou.fn.portal.vo.OuterCampus;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/4/18.
 */
@Service
public class OuterCampusServiceImpl implements IOuterCampusService {

    @Resource
    private MongoTemplate mongoTemplate;


    @Override
    public List<OuterCampus> findCampusByPosition(Double distance,Double longitude, Double latitude) {
        Criteria criteria = new Criteria();
        Query query=new Query();
        Point point=new Point(longitude,latitude);
        query.addCriteria(criteria.where("campusLocation").near(point).maxDistance(distance/1000/111));//公里数除以111算出弧度
        return   mongoTemplate.find(query,OuterCampus.class);
    }

    @Override
    public OuterCampus findOneNearest(Double longitude, Double latitude) {
        Criteria criteria = new Criteria();
        Query query=new Query();
        Point point=new Point(longitude,latitude);
        query.addCriteria(criteria.where("campusLocation").near(point));
        return   mongoTemplate.findOne(query,OuterCampus.class);
    }

    @Override
    public OuterCampus findById(Long campusId) {
        Query query=new Query();
        query.addCriteria(Criteria.where("campusId").is(campusId));
        return mongoTemplate.findOne(query,OuterCampus.class);
    }
}
